package com.bytz.modules.cms.util;

import org.junit.jupiter.api.Assertions;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class EntityChecker<E> extends ArrayList<EntityCheckerItem<E>> {

    @SafeVarargs
    public EntityChecker(EntityCheckerItem<E>... checkers) {
        this.addAll(Arrays.asList(checkers));
    }

    public void doCheck(List<E> data) {
        for (E datum : data) {
            boolean checked = false;
            for (EntityCheckerItem<E> checker : this) {
                if (checker.getPredicator().test(datum)) {
                    checker.getChecker().accept(datum);
                    checked = true;
                }
            }

            if (!checked) {
                Assertions.fail("数据没有匹配到检查器" + datum);
            }
        }
    }
}
